﻿using System;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Rendering;
//using UnityEngine.VR;
using Valve.VR;

// Token: 0x0200080F RID: 2063
public class SteamVR : IDisposable
{
	// Token: 0x06002F53 RID: 12115 RVA: 0x000F4688 File Offset: 0x000F2888
	private SteamVR()
	{
		this.hmd = OpenVR.System;
		GlobalData.Debug_Log("Connected to " + this.hmd_TrackingSystemName + ":" + this.hmd_SerialNumber);
		this.compositor = OpenVR.Compositor;
		this.overlay = OpenVR.Overlay;
		uint num = 0U;
		uint num2 = 0U;
		this.hmd.GetRecommendedRenderTargetSize(ref num, ref num2);
		this.sceneWidth = num;
		this.sceneHeight = num2;
		float num3 = 0f;
		float num4 = 0f;
		float num5 = 0f;
		float num6 = 0f;
		this.hmd.GetProjectionRaw(EVREye.Eye_Left, ref num3, ref num4, ref num5, ref num6);
		float num7 = 0f;
		float num8 = 0f;
		float num9 = 0f;
		float num10 = 0f;
		this.hmd.GetProjectionRaw(EVREye.Eye_Right, ref num7, ref num8, ref num9, ref num10);
		this.tanHalfFov = new Vector2(Mathf.Max(new float[]
		{
			-num3,
			num4,
			-num7,
			num8
		}), Mathf.Max(new float[]
		{
			-num5,
			num6,
			-num9,
			num10
		}));
		this.textureBounds = new VRTextureBounds_t[2];
		this.textureBounds[0].uMin = 0.5f + 0.5f * num3 / this.tanHalfFov.x;
		this.textureBounds[0].uMax = 0.5f + 0.5f * num4 / this.tanHalfFov.x;
		this.textureBounds[0].vMin = 0.5f - 0.5f * num6 / this.tanHalfFov.y;
		this.textureBounds[0].vMax = 0.5f - 0.5f * num5 / this.tanHalfFov.y;
		this.textureBounds[1].uMin = 0.5f + 0.5f * num7 / this.tanHalfFov.x;
		this.textureBounds[1].uMax = 0.5f + 0.5f * num8 / this.tanHalfFov.x;
		this.textureBounds[1].vMin = 0.5f - 0.5f * num10 / this.tanHalfFov.y;
		this.textureBounds[1].vMax = 0.5f - 0.5f * num9 / this.tanHalfFov.y;
		this.sceneWidth /= Mathf.Max(this.textureBounds[0].uMax - this.textureBounds[0].uMin, this.textureBounds[1].uMax - this.textureBounds[1].uMin);
		this.sceneHeight /= Mathf.Max(this.textureBounds[0].vMax - this.textureBounds[0].vMin, this.textureBounds[1].vMax - this.textureBounds[1].vMin);
		this.aspect = this.tanHalfFov.x / this.tanHalfFov.y;
		this.fieldOfView = 2f * Mathf.Atan(this.tanHalfFov.y) * 57.29578f;
		this.eyes = new SteamVR_Utils.RigidTransform[]
		{
			new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Left)),
			new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Right))
		};
		GraphicsDeviceType graphicsDeviceType = SystemInfo.graphicsDeviceType;
		switch (graphicsDeviceType)
		{
		case GraphicsDeviceType.OpenGLES2:
		case GraphicsDeviceType.OpenGLES3:
			break;
		default:
			if (graphicsDeviceType != GraphicsDeviceType.OpenGLCore)
			{
				if (graphicsDeviceType != GraphicsDeviceType.Vulkan)
				{
					this.textureType = ETextureType.DirectX;
					goto IL_433;
				}
				this.textureType = ETextureType.Vulkan;
				goto IL_433;
			}
			break;
		}
		this.textureType = ETextureType.OpenGL;
		IL_433:
		SteamVR_Events.Initializing.Listen(new UnityAction<bool>(this.OnInitializing));
		SteamVR_Events.Calibrating.Listen(new UnityAction<bool>(this.OnCalibrating));
		SteamVR_Events.OutOfRange.Listen(new UnityAction<bool>(this.OnOutOfRange));
		SteamVR_Events.DeviceConnected.Listen(new UnityAction<int, bool>(this.OnDeviceConnected));
		SteamVR_Events.NewPoses.Listen(new UnityAction<TrackedDevicePose_t[]>(this.OnNewPoses));
	}

	// Token: 0x17000507 RID: 1287
	// (get) Token: 0x06002F54 RID: 12116 RVA: 0x000F4B38 File Offset: 0x000F2D38
	public static bool active
	{
		get
		{
			return SteamVR._instance != null;
		}
	}

	// Token: 0x17000508 RID: 1288
	// (get) Token: 0x06002F55 RID: 12117 RVA: 0x000F4B48 File Offset: 0x000F2D48
	// (set) Token: 0x06002F56 RID: 12118 RVA: 0x000F4B60 File Offset: 0x000F2D60
	public static bool enabled
	{
		get
		{
			//if (!VRSettings.enabled)
			{
				SteamVR.enabled = false;
			}
			return SteamVR._enabled;
		}
		set
		{
			SteamVR._enabled = value;
			if (!SteamVR._enabled)
			{
				SteamVR.SafeDispose();
			}
		}
	}

	// Token: 0x17000509 RID: 1289
	// (get) Token: 0x06002F57 RID: 12119 RVA: 0x000F4B78 File Offset: 0x000F2D78
	public static SteamVR instance
	{
		get
		{
			if (!SteamVR.enabled)
			{
				return null;
			}
			if (SteamVR._instance == null)
			{
				SteamVR._instance = SteamVR.CreateInstance();
				if (SteamVR._instance == null)
				{
					SteamVR._enabled = false;
				}
			}
			return SteamVR._instance;
		}
	}

	// Token: 0x1700050A RID: 1290
	// (get) Token: 0x06002F58 RID: 12120 RVA: 0x000F4BB0 File Offset: 0x000F2DB0
	public static bool usingNativeSupport
	{
		get
		{
			//return VRDevice.GetNativePtr() != IntPtr.Zero;
			return false;
		}
	}

	// Token: 0x06002F59 RID: 12121 RVA: 0x000F4BC4 File Offset: 0x000F2DC4
	private static SteamVR CreateInstance()
	{
		try
		{
			EVRInitError evrinitError = EVRInitError.None;
			if (!SteamVR.usingNativeSupport)
			{
				GlobalData.Debug_Log("OpenVR initialization failed.  Ensure 'Virtual Reality Supported' is checked in Player Settings, and OpenVR is added to the list of Virtual Reality SDKs.");
				return null;
			}
			OpenVR.GetGenericInterface("IVRCompositor_020", ref evrinitError);
			if (evrinitError != EVRInitError.None)
			{
				SteamVR.ReportError(evrinitError);
				return null;
			}
			OpenVR.GetGenericInterface("IVROverlay_016", ref evrinitError);
			if (evrinitError != EVRInitError.None)
			{
				SteamVR.ReportError(evrinitError);
				return null;
			}
		}
		catch (Exception message)
		{
			GlobalData.Debug_LogError(message);
			return null;
		}
		return new SteamVR();
	}

	// Token: 0x06002F5A RID: 12122 RVA: 0x000F4C58 File Offset: 0x000F2E58
	private static void ReportError(EVRInitError error)
	{
		if (error != EVRInitError.None)
		{
			if (error != EVRInitError.Init_VRClientDLLNotFound)
			{
				if (error != EVRInitError.Driver_RuntimeOutOfDate)
				{
					if (error != EVRInitError.VendorSpecific_UnableToConnectToOculusRuntime)
					{
						GlobalData.Debug_Log(OpenVR.GetStringForHmdError(error));
					}
					else
					{
						GlobalData.Debug_Log("SteamVR Initialization Failed!  Make sure device is on, Oculus runtime is installed, and OVRService_*.exe is running.");
					}
				}
				else
				{
					GlobalData.Debug_Log("SteamVR Initialization Failed!  Make sure device's runtime is up to date.");
				}
			}
			else
			{
				GlobalData.Debug_Log("SteamVR drivers not found!  They can be installed via Steam under Library > Tools.  Visit http://steampowered.com to install Steam.");
			}
		}
	}

	// Token: 0x1700050B RID: 1291
	// (get) Token: 0x06002F5B RID: 12123 RVA: 0x000F4CD0 File Offset: 0x000F2ED0
	// (set) Token: 0x06002F5C RID: 12124 RVA: 0x000F4CD8 File Offset: 0x000F2ED8
	public CVRSystem hmd { get; private set; }

	// Token: 0x1700050C RID: 1292
	// (get) Token: 0x06002F5D RID: 12125 RVA: 0x000F4CE4 File Offset: 0x000F2EE4
	// (set) Token: 0x06002F5E RID: 12126 RVA: 0x000F4CEC File Offset: 0x000F2EEC
	public CVRCompositor compositor { get; private set; }

	// Token: 0x1700050D RID: 1293
	// (get) Token: 0x06002F5F RID: 12127 RVA: 0x000F4CF8 File Offset: 0x000F2EF8
	// (set) Token: 0x06002F60 RID: 12128 RVA: 0x000F4D00 File Offset: 0x000F2F00
	public CVROverlay overlay { get; private set; }

	// Token: 0x1700050E RID: 1294
	// (get) Token: 0x06002F61 RID: 12129 RVA: 0x000F4D0C File Offset: 0x000F2F0C
	// (set) Token: 0x06002F62 RID: 12130 RVA: 0x000F4D14 File Offset: 0x000F2F14
	public static bool initializing { get; private set; }

	// Token: 0x1700050F RID: 1295
	// (get) Token: 0x06002F63 RID: 12131 RVA: 0x000F4D1C File Offset: 0x000F2F1C
	// (set) Token: 0x06002F64 RID: 12132 RVA: 0x000F4D24 File Offset: 0x000F2F24
	public static bool calibrating { get; private set; }

	// Token: 0x17000510 RID: 1296
	// (get) Token: 0x06002F65 RID: 12133 RVA: 0x000F4D2C File Offset: 0x000F2F2C
	// (set) Token: 0x06002F66 RID: 12134 RVA: 0x000F4D34 File Offset: 0x000F2F34
	public static bool outOfRange { get; private set; }

	// Token: 0x17000511 RID: 1297
	// (get) Token: 0x06002F67 RID: 12135 RVA: 0x000F4D3C File Offset: 0x000F2F3C
	// (set) Token: 0x06002F68 RID: 12136 RVA: 0x000F4D44 File Offset: 0x000F2F44
	public float sceneWidth { get; private set; }

	// Token: 0x17000512 RID: 1298
	// (get) Token: 0x06002F69 RID: 12137 RVA: 0x000F4D50 File Offset: 0x000F2F50
	// (set) Token: 0x06002F6A RID: 12138 RVA: 0x000F4D58 File Offset: 0x000F2F58
	public float sceneHeight { get; private set; }

	// Token: 0x17000513 RID: 1299
	// (get) Token: 0x06002F6B RID: 12139 RVA: 0x000F4D64 File Offset: 0x000F2F64
	// (set) Token: 0x06002F6C RID: 12140 RVA: 0x000F4D6C File Offset: 0x000F2F6C
	public float aspect { get; private set; }

	// Token: 0x17000514 RID: 1300
	// (get) Token: 0x06002F6D RID: 12141 RVA: 0x000F4D78 File Offset: 0x000F2F78
	// (set) Token: 0x06002F6E RID: 12142 RVA: 0x000F4D80 File Offset: 0x000F2F80
	public float fieldOfView { get; private set; }

	// Token: 0x17000515 RID: 1301
	// (get) Token: 0x06002F6F RID: 12143 RVA: 0x000F4D8C File Offset: 0x000F2F8C
	// (set) Token: 0x06002F70 RID: 12144 RVA: 0x000F4D94 File Offset: 0x000F2F94
	public Vector2 tanHalfFov { get; private set; }

	// Token: 0x17000516 RID: 1302
	// (get) Token: 0x06002F71 RID: 12145 RVA: 0x000F4DA0 File Offset: 0x000F2FA0
	// (set) Token: 0x06002F72 RID: 12146 RVA: 0x000F4DA8 File Offset: 0x000F2FA8
	public VRTextureBounds_t[] textureBounds { get; private set; }

	// Token: 0x17000517 RID: 1303
	// (get) Token: 0x06002F73 RID: 12147 RVA: 0x000F4DB4 File Offset: 0x000F2FB4
	// (set) Token: 0x06002F74 RID: 12148 RVA: 0x000F4DBC File Offset: 0x000F2FBC
	public SteamVR_Utils.RigidTransform[] eyes { get; private set; }

	// Token: 0x17000518 RID: 1304
	// (get) Token: 0x06002F75 RID: 12149 RVA: 0x000F4DC8 File Offset: 0x000F2FC8
	public string hmd_TrackingSystemName
	{
		get
		{
			return this.GetStringProperty(ETrackedDeviceProperty.Prop_TrackingSystemName_String, 0U);
		}
	}

	// Token: 0x17000519 RID: 1305
	// (get) Token: 0x06002F76 RID: 12150 RVA: 0x000F4DD8 File Offset: 0x000F2FD8
	public string hmd_ModelNumber
	{
		get
		{
			return this.GetStringProperty(ETrackedDeviceProperty.Prop_ModelNumber_String, 0U);
		}
	}

	// Token: 0x1700051A RID: 1306
	// (get) Token: 0x06002F77 RID: 12151 RVA: 0x000F4DE8 File Offset: 0x000F2FE8
	public string hmd_SerialNumber
	{
		get
		{
			return this.GetStringProperty(ETrackedDeviceProperty.Prop_SerialNumber_String, 0U);
		}
	}

	// Token: 0x1700051B RID: 1307
	// (get) Token: 0x06002F78 RID: 12152 RVA: 0x000F4DF8 File Offset: 0x000F2FF8
	public float hmd_SecondsFromVsyncToPhotons
	{
		get
		{
			return this.GetFloatProperty(ETrackedDeviceProperty.Prop_SecondsFromVsyncToPhotons_Float, 0U);
		}
	}

	// Token: 0x1700051C RID: 1308
	// (get) Token: 0x06002F79 RID: 12153 RVA: 0x000F4E08 File Offset: 0x000F3008
	public float hmd_DisplayFrequency
	{
		get
		{
			return this.GetFloatProperty(ETrackedDeviceProperty.Prop_DisplayFrequency_Float, 0U);
		}
	}

	// Token: 0x06002F7A RID: 12154 RVA: 0x000F4E18 File Offset: 0x000F3018
	public string GetTrackedDeviceString(uint deviceId)
	{
		ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
		uint stringTrackedDeviceProperty = this.hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_AttachedDeviceId_String, null, 0U, ref etrackedPropertyError);
		if (stringTrackedDeviceProperty > 1U)
		{
			StringBuilder stringBuilder = new StringBuilder((int)stringTrackedDeviceProperty);
			this.hmd.GetStringTrackedDeviceProperty(deviceId, ETrackedDeviceProperty.Prop_AttachedDeviceId_String, stringBuilder, stringTrackedDeviceProperty, ref etrackedPropertyError);
			return stringBuilder.ToString();
		}
		return null;
	}

	// Token: 0x06002F7B RID: 12155 RVA: 0x000F4E6C File Offset: 0x000F306C
	public string GetStringProperty(ETrackedDeviceProperty prop, uint deviceId = 0U)
	{
		ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
		uint stringTrackedDeviceProperty = this.hmd.GetStringTrackedDeviceProperty(deviceId, prop, null, 0U, ref etrackedPropertyError);
		if (stringTrackedDeviceProperty > 1U)
		{
			StringBuilder stringBuilder = new StringBuilder((int)stringTrackedDeviceProperty);
			this.hmd.GetStringTrackedDeviceProperty(deviceId, prop, stringBuilder, stringTrackedDeviceProperty, ref etrackedPropertyError);
			return stringBuilder.ToString();
		}
		return (etrackedPropertyError == ETrackedPropertyError.TrackedProp_Success) ? "<unknown>" : etrackedPropertyError.ToString();
	}

	// Token: 0x06002F7C RID: 12156 RVA: 0x000F4ED4 File Offset: 0x000F30D4
	public float GetFloatProperty(ETrackedDeviceProperty prop, uint deviceId = 0U)
	{
		ETrackedPropertyError etrackedPropertyError = ETrackedPropertyError.TrackedProp_Success;
		return this.hmd.GetFloatTrackedDeviceProperty(deviceId, prop, ref etrackedPropertyError);
	}

	// Token: 0x06002F7D RID: 12157 RVA: 0x000F4EF4 File Offset: 0x000F30F4
	private void OnInitializing(bool initializing)
	{
		SteamVR.initializing = initializing;
	}

	// Token: 0x06002F7E RID: 12158 RVA: 0x000F4EFC File Offset: 0x000F30FC
	private void OnCalibrating(bool calibrating)
	{
		SteamVR.calibrating = calibrating;
	}

	// Token: 0x06002F7F RID: 12159 RVA: 0x000F4F04 File Offset: 0x000F3104
	private void OnOutOfRange(bool outOfRange)
	{
		SteamVR.outOfRange = outOfRange;
	}

	// Token: 0x06002F80 RID: 12160 RVA: 0x000F4F0C File Offset: 0x000F310C
	private void OnDeviceConnected(int i, bool connected)
	{
		SteamVR.connected[i] = connected;
	}

	// Token: 0x06002F81 RID: 12161 RVA: 0x000F4F18 File Offset: 0x000F3118
	private void OnNewPoses(TrackedDevicePose_t[] poses)
	{
		this.eyes[0] = new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Left));
		this.eyes[1] = new SteamVR_Utils.RigidTransform(this.hmd.GetEyeToHeadTransform(EVREye.Eye_Right));
		for (int i = 0; i < poses.Length; i++)
		{
			bool bDeviceIsConnected = poses[i].bDeviceIsConnected;
			if (bDeviceIsConnected != SteamVR.connected[i])
			{
				SteamVR_Events.DeviceConnected.Send(i, bDeviceIsConnected);
			}
		}
		if ((long)poses.Length > 0L)
		{
			ETrackingResult eTrackingResult = poses[(int)((UIntPtr)0)].eTrackingResult;
			bool flag = eTrackingResult == ETrackingResult.Uninitialized;
			if (flag != SteamVR.initializing)
			{
				SteamVR_Events.Initializing.Send(flag);
			}
			bool flag2 = eTrackingResult == ETrackingResult.Calibrating_InProgress || eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
			if (flag2 != SteamVR.calibrating)
			{
				SteamVR_Events.Calibrating.Send(flag2);
			}
			bool flag3 = eTrackingResult == ETrackingResult.Running_OutOfRange || eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
			if (flag3 != SteamVR.outOfRange)
			{
				SteamVR_Events.OutOfRange.Send(flag3);
			}
		}
	}

	// Token: 0x06002F82 RID: 12162 RVA: 0x000F5030 File Offset: 0x000F3230
	~SteamVR()
	{
		this.Dispose(false);
	}

	// Token: 0x06002F83 RID: 12163 RVA: 0x000F5060 File Offset: 0x000F3260
	public void Dispose()
	{
		this.Dispose(true);
		GC.SuppressFinalize(this);
	}

	// Token: 0x06002F84 RID: 12164 RVA: 0x000F5070 File Offset: 0x000F3270
	private void Dispose(bool disposing)
	{
		SteamVR_Events.Initializing.Remove(new UnityAction<bool>(this.OnInitializing));
		SteamVR_Events.Calibrating.Remove(new UnityAction<bool>(this.OnCalibrating));
		SteamVR_Events.OutOfRange.Remove(new UnityAction<bool>(this.OnOutOfRange));
		SteamVR_Events.DeviceConnected.Remove(new UnityAction<int, bool>(this.OnDeviceConnected));
		SteamVR_Events.NewPoses.Remove(new UnityAction<TrackedDevicePose_t[]>(this.OnNewPoses));
		SteamVR._instance = null;
	}

	// Token: 0x06002F85 RID: 12165 RVA: 0x000F50F4 File Offset: 0x000F32F4
	public static void SafeDispose()
	{
		if (SteamVR._instance != null)
		{
			SteamVR._instance.Dispose();
		}
	}

	// Token: 0x04002F6A RID: 12138
	private static bool _enabled = true;

	// Token: 0x04002F6B RID: 12139
	private static SteamVR _instance;

	// Token: 0x04002F72 RID: 12146
	public static bool[] connected = new bool[16];

	// Token: 0x04002F7A RID: 12154
	public ETextureType textureType;
}
